﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* the Player Status-Box view */

package monopoly.views {
	
	import flash.display.Sprite;
	import fl.controls.Label;
	import fl.controls.TextArea;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.*;
	import monopoly.Player.Player;
	import monopoly.IFS.GLOBALS
	
	public class PlayerStatusBox extends Sprite
		{
		protected var player:Player;
		protected var _StatusBox:TextArea;
		protected var _status:Boolean = false;
		protected var _pLogo:Loader;
		protected var _url:URLRequest;
	
		public function PlayerStatusBox(player:Player)
			{
			this.player = player;
			
			_pLogo = new Loader();
			_StatusBox = new TextArea();
			
			configureListeners(_pLogo.contentLoaderInfo);
			var urlStr:String = "http://" + GLOBALS.WebServerName + "/" + player.Logo;
			
			if (GLOBALS.DEBUG & GLOBALS.DEBUG_LOGIC)
				trace("[PlayerStatusBox][constructor]: user= " + player.Name + " URL = " + urlStr);
				
			_url = new URLRequest(urlStr);
			}
		public function load():void
			{
			_pLogo.load(_url);
			_pLogo.cacheAsBitmap = true;
			}
			
		private function configureListeners(dispatcher:IEventDispatcher):void
			{
			dispatcher.addEventListener(Event.COMPLETE, onLogoComplete);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			}
		private function removeListeners(dispatcher:IEventDispatcher):void 
			{
            dispatcher.removeEventListener(Event.COMPLETE, onLogoComplete);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	        }
			
		private function onLogoComplete(event:Event):void
			{
			if (GLOBALS.DEBUG & GLOBALS.DEBUG_VIEW)
				trace("[PlayerStatusBox][onLogoComplete] Here we are in the center of status box, everybody is here again...");
			
			_pLogo.content.width = 50;
			_pLogo.content.height= 55;
			_pLogo.x = 1;
			_pLogo.y = 1;
			_pLogo.visible	= false;
			
			setupStatusBox();
			
			_StatusBox.x = x+_pLogo.content.width;
			
			if (GLOBALS.DEBUG & GLOBALS.DEBUG_VIEW)
				trace("[PlayerStatusBox][onLogoComplete] PlayerStatus.X = " + x + " and y = " + y);
				
			addChild(_StatusBox);
			addChild(_pLogo.content);

			removeListeners(_pLogo.contentLoaderInfo);
			
			this.visible = false;
			
			dispatchEvent(new Event(Event.COMPLETE));
			}
			
		public function setupStatusBox():void
			{
			_StatusBox.editable = false;
			_StatusBox.name		= player.Name;
			_StatusBox.setSize(150,100);
			_StatusBox.alpha = 1;
			
			_StatusBox.visible = false;
			}
			
		public function draw_statusBox():void
			{
			_StatusBox.text = _StatusBox.name + "\n";
			_StatusBox.appendText("Cash: " + player.Cash);

			_StatusBox.appendText("\nPlayer details: \n");
			_StatusBox.appendText("Age: " + player.Age + "\n");
			_StatusBox.appendText("City: " + player.City + "\n");
			_StatusBox.appendText("Country: " + player.Country + "\n");
			_StatusBox.appendText("Gender: " + player.Gender);
			
			_StatusBox.visible	= true;
			_pLogo.visible		= true;
			this.visible		= true;
			
			if (_status == true)
				{
				/* do some animation magic... */
				_StatusBox.appendText("\n is Playing");
				}
			else
				{
				/* undo animation magic... */
				}
			} 
		public override function set width(value:Number):void
			{
			_StatusBox.setSize(width-50,100);
			super.width = value;
			}
		
		public function undraw_statusBox():void
			{
			_StatusBox.visible	= false;
			_pLogo.visible		= false;
			this.visible		= false;
			}
		public function change_status():void
			{
			(_status == false) ? (_status = true) : (_status = false);
			}
		public function get Status():Boolean
			{
			return _status;
			}
			
        private function ioErrorHandler(event:IOErrorEvent):void {
			if (GLOBALS.DEBUG & GLOBALS.DEBUG_LOGIC)
	           trace("ioErrorHandler: " + event);
        	}

		}
}
